﻿using DMSkin.WPF;
using log4net;
using MXComponent.DAL;
using MXComponent.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MXComponent.Views.material
{
    /// <summary>
    /// materialPage.xaml 的交互逻辑
    /// </summary>
    public partial class materialPage : DMSkinSimpleWindow
    {
        public static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private materialDB mdb = new materialDB();
        private int? materialid = null;

        public materialPage()
        {
            InitializeComponent();
        }

        public materialPage(int materialid)
        {
            InitializeComponent();
            this.materialid = materialid;
            materialData model = mdb.GetModel(materialid);
            this.txtMaterialcode.Text = model.materialcode;
            this.txtMaterialname.Text = model.materialname;
            this.txtmaterialunit.Text = model.materialunit;
            this.txtmaterialdesc.Text = model.materialdesc;
        }

        private void Btnclose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Btnok_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtMaterialcode.Text))
            {
                MessageBox.Show("物料编码不能为空！");
                this.txtMaterialcode.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(this.txtMaterialname.Text))
            {
                MessageBox.Show("物料名称不能为空！");
                this.txtMaterialname.Focus();
                return;
            }
            try
            {
                materialData model = new materialData();
                model.materialcode = this.txtMaterialcode.Text.Trim();
                model.materialname = this.txtMaterialname.Text.Trim();
                model.materialunit = this.txtmaterialunit.Text.Trim();
                model.materialdesc = this.txtmaterialdesc.Text.Trim();
                model.active = 1;
                if (this.materialid != null)
                {
                    model.materialid = int.Parse(this.materialid.ToString());
                    model.updateby = GlobalClass.user.userid;
                    model.updatetime = DateTime.Now.ToString();
                    mdb.ModifyRecord(model);
                }
                else
                {
                    model.creatby = GlobalClass.user.userid;
                    model.createtime = DateTime.Now.ToString();
                    mdb.AddRecord(model);
                }

                MessageBox.Show("保存成功!");
                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                MessageBox.Show(ex.Message);
            }
        }
    }
}